package com.yonyou.iuap.shell.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.yonyou.iuap.pub.DateUtil;
import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.dao.BdCompatibleRelationEntityDao;
import com.yonyou.iuap.shell.dao.BdIndustrychartProductEntityDao;
import com.yonyou.iuap.shell.dao.BdProductEntityDao;
import com.yonyou.iuap.shell.entity.BdProductEntity;
import com.yonyou.iuap.shell.entity.exp.BdProductPoJo;
import com.yonyou.iuap.shell.entity.exp.ShellProductPoJo;
import com.yonyou.iuap.shell.pub.IShellDaoEnum;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IProductService;
import com.yonyou.iuap.shell.util.MathUtil3;
/**
 * 壳牌产品维护
 * 
 * **/
@Service("service-product")
public class ProductServiceImpl implements IProductService {

	@Autowired
	@Qualifier(IShellDaoEnum.Shell_Dao_Product)
	private BdProductEntityDao<BdProductEntity> bdProductEntityDao;
	@Autowired
	@Qualifier(IShellDaoEnum.Shell_Dao_Compatiblerealtion)
	private BdCompatibleRelationEntityDao comDao;
	@Autowired
	@Qualifier(IShellDaoEnum.Shell_Dao_Industrychartchartproduct)
	private BdIndustrychartProductEntityDao bdIndustrychartDao;
	
	public ResultDto queryCondition(RequestDto reqDto) {
		ResultDto rs = new ResultDto();
		List<BdProductEntity> listvo = bdProductEntityDao.selectAll();
		rs.setNum(listvo==null?0:listvo.size());
		rs.setStatus(0);
		rs.setMessage("数据查询成功!");
		rs.setData(listvo);
		return rs;
	}

	public ResultDto queryByCondition(String paramString) {
		ResultDto rs = new ResultDto();;
		List<BdProductEntity> listvo = bdProductEntityDao.selectByCondition(paramString);
		rs.setNum(listvo==null?0:listvo.size());
		rs.setStatus(0);
		rs.setMessage("数据查询成功!");
		rs.setData(JsonUtils.toJsonStr(listvo));
		return rs;
	}

	public ResultDto deleteByPrimaryKey(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("删除条件不能为空!");
		}else{
//			删除产品
			int i_del = bdProductEntityDao.deleteByPrimaryKey(paramString);
//			删除产品与竞品对应关系
			comDao.deleteByCondition(" pk_product='"+paramString+"'");
//			删除产品与部件关系
			bdIndustrychartDao.deleteByCondition(" pk_product='"+paramString+"'");
			rsDto.setStatus(0);
			rsDto.setMessage("删除完成!");
		}
		return rsDto;
	}

	public ResultDto insert(BdProductEntity supplier) {
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			supplier.setPk_product(MathUtil3.uuid32());
			supplier.setDr(0);
			supplier.setTs(DateUtil.getDateTime());
			supplier.setDtcreate(new Date());
			//判断产品名称是否存在
			int count = bdProductEntityDao.countByCondition(" product_name ='"+supplier.getProduct_name()+"'");
			if(count>0){
				rsDto.setStatus(1);
				rsDto.setMessage("产品名称已经存在!");
			}else{
				int a=  bdProductEntityDao.insertSelective(supplier);
				if(a>0){
					rsDto.setStatus(0);
					rsDto.setMessage("添加成功！");
				}
				else{
					rsDto.setStatus(1);
					rsDto.setMessage("添加失败！");
				}
			}
		}
			 return rsDto;
	}

	public ResultDto selectByPrimaryKey(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			BdProductEntity  listvo= (BdProductEntity) bdProductEntityDao.selectByPrimaryKey(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setData(listvo);
		}
		return rsDto;
	}

	public ResultDto selectAll() {
		ResultDto rsDto = new ResultDto();
		List<BdProductEntity>  listvo= bdProductEntityDao.queryAll();
		rsDto.setStatus(0);
		rsDto.setMessage("查询成功!");
		rsDto.setData(listvo);
		rsDto.setNum(listvo==null?0:listvo.size());
		rsDto.setCount(listvo==null?0:listvo.size());
	
		return rsDto;
		}


	public ResultDto selectByCondition(String paramString) {
		ResultDto rsDto = new ResultDto();
		List<BdProductEntity>  listvo= bdProductEntityDao.selectByCondition(paramString);
		rsDto.setStatus(0);
		rsDto.setMessage("查询成功!");
		rsDto.setData(listvo);
		rsDto.setNum(listvo==null?0:listvo.size());
		rsDto.setCount(listvo==null?0:listvo.size());
	
		return rsDto;
		
	}

	public ResultDto countByCondition(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			 int i_del =bdProductEntityDao.countByCondition(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setNum(i_del);
		}
		return rsDto;
	}

	public ResultDto deleteByCondition(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("删除条件不能为空!");
		}else{
			int i_del = bdProductEntityDao.deleteByCondition(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("删除完成!");
		}
		return rsDto;
	}

	public ResultDto insertSelective(BdProductEntity supplier) {
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			supplier.setPk_product(MathUtil3.uuid32());
			int a=  bdProductEntityDao.insertSelective(supplier);
			if(a>0){
				rsDto.setStatus(0);
				rsDto.setMessage("添加成功！");
			}
			}else{
				rsDto.setStatus(1);
				rsDto.setMessage("添加失败！");
			}
			 return rsDto;
	}

	public ResultDto updateByPrimaryKeySelective(BdProductEntity supplier) {
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			//判断修改的名称是否存在
			int count = bdProductEntityDao.countByCondition(" product_name ='"+supplier.getProduct_name()+"' and pk_product!='"+supplier.getPk_product()+"'");
			if(count>0){
				rsDto.setStatus(1);
				rsDto.setMessage("该名称已经存在！");
			}else{
				int i_del = bdProductEntityDao.updateByPrimaryKeySelective(supplier);
				rsDto.setStatus(0);
				rsDto.setMessage("修改完成!");
			}
			
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("修改失败!");
		}
		return rsDto;
	}

	public ResultDto updateByPrimaryKey(BdProductEntity supplier) {
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			int i_del = bdProductEntityDao.updateByPrimaryKey(supplier);
			rsDto.setStatus(0);
			rsDto.setMessage("修改完成!");
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("修改失败!");
		}
		return rsDto;
	}

	public ResultDto queryProduct(Map<String, String> param) {
		ResultDto rsDto = new ResultDto();
		if(param==null||StringUtils.isEmpty(param.get("pk_industry_chart"))){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			List<BdProductPoJo>  listvo= bdProductEntityDao.queryProduct(param);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setData(listvo);
		}
		return rsDto;
	}

	public ResultDto pageQuery(RequestDto reqDto) {
		ResultDto rsDto = new ResultDto();
		List<ShellProductPoJo> listvo = bdProductEntityDao.pageQuery(reqDto);
		rsDto.setData(listvo);
		rsDto.setMessage("数据查询成功!");
		rsDto.setStatus(0);
		rsDto.setNum(listvo==null?0:listvo.size());
		//查询汇总数
		int count = bdProductEntityDao.pageQueryCount(reqDto);
		rsDto.setCount(count);
		return rsDto;
	}

	public ResultDto pageQueryProduct(Map reqDto) {
		
		ResultDto rsDto = new ResultDto();
		List<BdProductPoJo>  listvo  = bdProductEntityDao.pageQueryProduct(reqDto);
		rsDto.setData(listvo);
		rsDto.setMessage("数据查询成功!");
		rsDto.setStatus(0);
		rsDto.setNum(listvo==null?0:listvo.size());
		int count = bdProductEntityDao.pageQueryProductCount(reqDto);
		rsDto.setCount(count);
		return rsDto;
	}

	public ResultDto selectAllPage(Map reqDto) {
		
		ResultDto rs = new ResultDto();
		List<BdProductEntity> listvo = bdProductEntityDao.queryAllPage(reqDto);
		rs.setNum(listvo==null?0:listvo.size());
		rs.setStatus(0);
		rs.setMessage("数据查询成功!");
		rs.setData(listvo);
		List<BdProductEntity> listvo1 =bdProductEntityDao.queryAll();
		rs.setCount(listvo1.size());
		return rs;
	}

	public ResultDto selectByConditionPage(Map reqDto) {
		ResultDto rs = new ResultDto();
		
		List<BdProductEntity> listvo = bdProductEntityDao.selectByConditionPage(reqDto);
		rs.setNum(listvo==null?0:listvo.size());
		rs.setStatus(0);
		rs.setMessage("数据查询成功!");
		rs.setData(JsonUtils.toJsonStr(listvo));
		int count = bdProductEntityDao.selectByConditionPageCount(reqDto);
		rs.setCount(count);
		return rs;
	}

	



	
}
